package com.gogowan.agroenterprise.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.model.ChanPinDiatalBean;
import com.gogowan.agroenterprise.utils.INewAdapterClick;

import java.util.List;

public class ChanPinDiatalAdapter extends BaseAdapter {

	private final INewAdapterClick click;
	private Context context;
	private List<ChanPinDiatalBean> datas;

	public ChanPinDiatalAdapter(Context context, List<ChanPinDiatalBean> datas, INewAdapterClick click) {
		this.context = context;
		this.datas = datas;
		this.click=click;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(R.layout.chanpin_detail_item, null);
			viewHolder = new ViewHolder();
			viewHolder.ll_main =  convertView.findViewById(R.id.ll_main);
			viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			viewHolder.tv_picihao = (TextView) convertView.findViewById(R.id.tv_picihao);
			viewHolder.tv_shuliang = (TextView) convertView.findViewById(R.id.tv_shuliang);
			viewHolder.tv_baozhuangshuliang = (TextView) convertView.findViewById(R.id.tv_baozhuangshuliang);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		ChanPinDiatalBean chuKuDiatalBean = datas.get(position);
		viewHolder.tv_name.setText(chuKuDiatalBean.getProductName());
		viewHolder.tv_picihao.setText(chuKuDiatalBean.getBatchNumber());

		String str="";
		int number=0;
		for (int i = 0; i < chuKuDiatalBean.getPackageCountInfos().size(); i++) {
			ChanPinDiatalBean.PackageCountInfo packageCountInfo = (ChanPinDiatalBean.PackageCountInfo) chuKuDiatalBean.getPackageCountInfos().get(i);
			str+=packageCountInfo.getLevel()+"级产品共有"+packageCountInfo.getCount()+"个";
			number+=packageCountInfo.getCount();
			if(i+1!=chuKuDiatalBean.getPackageCountInfos().size()){
				str+=",";
			}
		}
		viewHolder.tv_shuliang.setText(number+"");
		viewHolder.tv_baozhuangshuliang.setText("包装信息："+str);
		viewHolder.ll_main.setOnClickListener(view -> {
			click.onItemClick(view,position);
		});
		return convertView;
	}

	static class ViewHolder {
		public View ll_main;
		public TextView tv_name,tv_picihao,tv_shuliang,tv_baozhuangshuliang;
	}

}
